package com.yang.core.config;

import com.yang.core.api.RestClientInterceptor;
import com.yang.core.model.RequestContext;
import com.yang.core.model.ResponseContext;
import com.yang.core.model.ExceptionContext;
import com.yang.core.config.RestClientProperties.Cache;
import lombok.extern.slf4j.Slf4j;

/**
 * RestClient缓存拦截器
 */
@Slf4j
public class RestClientCacheInterceptor implements RestClientInterceptor {

    private final Cache cacheConfig;

    public RestClientCacheInterceptor(Cache cacheConfig) {
        this.cacheConfig = cacheConfig;
        log.info("初始化RestClient缓存拦截器，TTL: {}分钟", cacheConfig.getDefaultTtl().toMinutes());
    }

    @Override
    public void preRequest(RequestContext context) {
        if (cacheConfig.isEnabled() && "GET".equalsIgnoreCase(context.getMethod())) {
            log.debug("缓存检查: {}", context.getUrl());
        }
    }

    @Override
    public void postResponse(ResponseContext context) {
        if (cacheConfig.isEnabled() && context.isSuccess()) {
            log.debug("缓存存储: {} - 响应已缓存", context.getStatusCode());
        }
    }

    @Override
    public void onError(ExceptionContext context) {
        log.debug("缓存错误处理: {}", context.getException().getMessage());
    }
}